package com.example.radioplayer;

import com.example.radioplayer.MusicLoader.Ablum;
import com.example.radioplayer.RadioPlayerService.MusicBinder;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class RadioFragment extends Fragment implements OnClickListener {
	View view;
	MusicBinder mBinder;
	private AudioManager mgr;
	private Ablum mAblum;

	public RadioFragment(Ablum ablum) {
		mAblum = ablum;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_music, container, false);
		// view.setBackgroundResource(bgResId);
		mgr = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
		
		initView();

		return view;
	}

	private void initView() {
		// TODO Auto-generated method stub
		BitmapDrawable drawable = new BitmapDrawable(mAblum.cover);
		((RelativeLayout)view.findViewById(R.id.musicplayer)).setBackground(drawable);
		//Button bt1=(Button)view.findViewById(R.id.btn_music_play);
		//bt1.setOnClickListener(this);
		((Button)view.findViewById(R.id.btn_music_play)).setOnClickListener(this);
		((Button)view.findViewById(R.id.btn_music_volume_down)).setOnClickListener(this);
		((Button)view.findViewById(R.id.btn_music_volume_up)).setOnClickListener(this);
		
		IntentFilter filter = new IntentFilter(RadioPlayerService.ACTION_UPDATE_CURRENT_MUSIC);
		getActivity().registerReceiver(musicBr, filter);
		
		registerHeadsetPlugReceiver(); 
	
	
	}

	private void registerHeadsetPlugReceiver() {
		// TODO Auto-generated method stub
		IntentFilter  filter = new IntentFilter(); 
		filter.addAction("android.intent.action.HEADSET_PLUG"); 
		 getActivity().registerReceiver(headsetPlugReceiver, filter);  
		
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		getActivity().unregisterReceiver(musicBr);
		getActivity().unregisterReceiver(headsetPlugReceiver);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		mBinder = ((MainActivity) getActivity()).getBinder();
		switch (view.getId()) {
		case R.id.btn_music_play:
			if (mBinder.isPlaying()) {
				((Button) view.findViewById(R.id.btn_music_play)).setBackgroundResource(R.drawable.btn_start);
				mBinder.stopPlay();
			} else {
				((Button) view.findViewById(R.id.btn_music_play)).setBackgroundResource(R.drawable.btn_stop);
				mBinder.startPlay();
			}
			break;

		case R.id.btn_music_volume_up:
			mgr.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			break;

		case R.id.btn_music_volume_down:
			mgr.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			break;
		default:
			break;
		}

	}

	public void changeMusicStatus(){
		mBinder = ((MainActivity) getActivity()).getBinder();
		if (mBinder.isPlaying()) {
			((Button) view.findViewById(R.id.btn_music_play)).setBackgroundResource(R.drawable.btn_stop);
			
		} else {
			((Button) view.findViewById(R.id.btn_music_play)).setBackgroundResource(R.drawable.btn_start);
			
		}
		
	}
	
	BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context arg0, Intent intent) {
			// TODO Auto-generated method stub
			
			if(intent.hasExtra("state")){  
                if(intent.getIntExtra("state", 0)==1){  
                    ((ImageView)view.findViewById(R.id.musicplayer_earplug)).setBackgroundResource(R.drawable.play_earpoor); 
                }  
                else if(intent.getIntExtra("state", 0)==0){  
                	((ImageView)view.findViewById(R.id.musicplayer_earplug)).setBackground(null); 
                }  
            }
		
		}
	};

	BroadcastReceiver musicBr = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			String ablumName = arg1.getStringExtra(RadioPlayerService.ACTION_ABLUM_NAME);
			if(ablumName.equals(mAblum.name)){
				int currentMusic=arg1.getIntExtra(RadioPlayerService.ACTION_UPDATE_CURRENT_MUSIC, 0);
				((TextView)view.findViewById(R.id.musicplayer_musicname)).setText(mAblum.musics.get(currentMusic).title);	
			}
		}
	};

}
